package server.util;

import org.aspectj.util.FileUtil;
import server.enitity.Order;
import server.lunch.AppLunch;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @ClassName TxtUtil
 * @Description
 * @Author Jingxiao.Ye
 * @Date 2021/12/20 上午2:10
 * @Version V1.0
 * @Since JDK 11
 */
public class TxtUtil {

    public static void main(String[] args) {
        List<Order> orders = AppLunch.buildOrderList(100_0000);
        List<String[]> strings = toStringArray(orders);
        StringBuilder sb = new StringBuilder();
        for (String[] string : strings) {
            StringBuilder sb1 = new StringBuilder();
            for (String s : string) {
                sb1.append("\"").append(s).append("\"\t");
            }
            sb1.deleteCharAt(sb1.length()-1);
            sb1.append("\n");
            sb.append(sb1);
        }
        String s = FileUtil.writeAsString(new File("/home/melt/java-study/t_order.txt"),sb.toString());
        System.out.println(s);
    }

    private static List<String[]> toStringArray(List<Order> orders) {
        List<String[]> records = new ArrayList<String[]>();
        //add header record
//        records.add(new String[]{"id","user_id","item_id","count","price_sum","price","pay_price","create_time","update_time","pay_type","order_ip","app_id","app_version","order_no",
//                "bussiness_id","status","app_platform","device","item_name","finish_time"});
        Iterator<Order> it = orders.iterator();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSS");
        while(it.hasNext()){
            Order order = it.next();
            records.add(new String[]{order.getId()+"",order.getUserId()+"",order.getItemId()+"",order.getCount()+"",order.getPriceSum()+"",order.getPrice()+"",
                    order.getPayPrice()+"",simpleDateFormat.format(order.getCreateTime()),simpleDateFormat.format(order.getUpdateTime()),order.getPayType()+"",order.getOrderIp()+"",order.getAppId()+"",order.getAppVersion()+"",
                    order.getOrderNo()+"",order.getBussinessId()+"",order.getStatus()+"",order.getAppPlatform()+"",order.getDevice(),order.getItemName(),order.getFinishTime()+""});
        }
        return records;
    }
}
